package com.lcy.eb.controller;

import com.lcy.eb.model.EbUser;
import com.lcy.eb.service.LoginService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class loginController {
    @RequestMapping("/to_login")
    public String toLogin(){
        System.out.println("进入了loginController的toLogin方法");
        return "login";
    }
    @Resource


    private LoginService loginService;
    @RequestMapping("/login")
    public String login(EbUser ebUser, Model model, String verycode, HttpServletRequest request){
        model.addAttribute("error","");
        if(!CaptchaUtil.ver(verycode,request)){
            CaptchaUtil.clear(request);
            model.addAttribute("error","验证码错误");
            return "forward:to_login";
        }
        EbUser lgnUser = loginService.login(ebUser);
        request.getSession().setAttribute("lgnUser",lgnUser);
        return "redirect:/index/index";
    }

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response)throws Exception{
        CaptchaUtil.out(request,response);
    }
}
